package cn.com.customer.manager.ui.message.notice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.EmailAttachmentAdapter
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.Attachments
import cn.com.customer.manager.bean.MessageListInfo
import cn.com.customer.manager.bean.Notice
import cn.com.customer.manager.bean.RequestBean
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import com.chad.library.adapter.base.BaseQuickAdapter
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.XHttpProxy
import com.xuexiang.xhttp2.annotation.ThreadType
import com.xuexiang.xhttp2.model.ApiResult
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_notice_detail.*

class NoticeDetailActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {
    private lateinit var mAdapter: EmailAttachmentAdapter
    private var mData: MutableList<Attachments> = mutableListOf()
    private lateinit var notice: Notice
    override fun getLayoutId(): Int = R.layout.activity_notice_detail

    override fun initView(savedInstanceState: Bundle?) {
        notice = intent.getParcelableExtra<Notice>("data")
        initActionBar("通知详情")
        initData()
    }

    private fun initData() {
        mData = notice.noticeAttachments as MutableList<Attachments>
        notice_detail_Type.text = notice.noticeTitle
        notice_detail_content.text = notice.noticeContent
        modifyState(notice.noticeId)
        initRv()
    }

    private fun initRv() {
        notice_detail_rv.layoutManager = GridLayoutManager(this, 5)
        mAdapter = EmailAttachmentAdapter(R.layout.item_attachment_list)
        notice_detail_rv.adapter = mAdapter
        mAdapter.onItemClickListener = this
        mAdapter.onItemChildClickListener = this
        mAdapter.addData(mData)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        BaseApi.downfile(mData[position].url)
    }

    private fun modifyState(noticeId:String) {
        var params: MutableMap<String, String> = mutableMapOf()
        params["noticeId"] = noticeId

        val request = XHttp.custom()
        request.create(BaseApi.IMessage::class.java).noticeModifyStatus(RequestBean(params)).subscribe(object :TipRequestSubscriber<Map<String, String>?>() {
            override fun onSuccess(apiResult: Map<String, String>?) {
                apiResult?.let {

                }
            }
        })
    }
}
